package com.dq.service.record.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.TrackQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.org.Org;
import com.dq.domain.record.MaintainRecord;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgService;
import com.dq.service.record.MaintainRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/2 15:04
 */
@Service
public class MaintainRecordServiceImpl extends BaseRepositories implements MaintainRecordService {

    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private OrgService orgService;

    @Override
    public List<MaintainRecord> findByWhere(TrackQuery trackQuery) throws AdminException {
        StringBuilder hql = new StringBuilder("select u from MaintainRecord u where u.deleted=false");
        Query q = createWhereHql(trackQuery,hql);
        q.setFirstResult((trackQuery.getPage()-1)*trackQuery.getSize());
        q.setMaxResults(trackQuery.getSize());
        List<MaintainRecord> maintainRecordList = q.getResultList();
        return maintainRecordList;
    }

    @Override
    public Long countByWhere(TrackQuery trackQuery) throws AdminException {
        StringBuilder hql = new StringBuilder("select count(u) from MaintainRecord u where u.deleted=false");
        Query q = createWhereHql(trackQuery,hql);
        return new Long((long) q.getResultList().get(0));
    }
    public Query createWhereHql(TrackQuery trackQuery,StringBuilder hql) throws AdminException {
        if(null!=trackQuery.getOrgId()){
            hql.append(" and u.sysDept=:sysdept");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.sysDept.id in ").append(orgIdsIn);
            }
        }

        if(StringUtils.isNotEmpty(trackQuery.getValue())){
            hql.append(" and (u.wxUser.cardNo like:value");
            hql.append(" or u.wxUser.username like:value");
            hql.append(" or u.wxUser.userRealcName like:value)");
        }

        if(StringUtils.isNotBlank(trackQuery.getWxUserCardNo())) {
            hql.append(" and u.wxUser.cardNo like '").append(BaseAdminQuery.likeSql(trackQuery.getWxUserCardNo())).append("'");
        }
        if(StringUtils.isNotBlank(trackQuery.getWxUserUserRealName())) {
            hql.append(" and u.wxUser.userRealcName like '").append(BaseAdminQuery.likeSql(trackQuery.getWxUserUserRealName())).append("'");
        }

        if(null!=trackQuery.getStatus()){
            hql.append(" and u.maintainStatus=:maintainStatus");
        }
        if(null!=trackQuery.getType()){
            hql.append(" and u.maintainType=:maintainType");
        }
        if(null!=trackQuery.getBeginTime()){
            hql.append(" and u.beginTime>=:beginTime");
        }
        if(null!=trackQuery.getEndTime()){
            hql.append(" and u.beginTime<=:endTime");
        }
        if(StringUtils.isNotEmpty(trackQuery.getOrgName())){
            hql.append(" and u.sysDept.name like:orgName");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query query = entityManager.createQuery(hql.toString());
        if(StringUtils.isNotEmpty(trackQuery.getValue())){
            query.setParameter("value","%"+trackQuery.getValue()+"%");
        }
        if(null!=trackQuery.getStatus()){
            query.setParameter("maintainStatus",trackQuery.getStatus());
        }
        if(null!=trackQuery.getType()){
            query.setParameter("maintainType",trackQuery.getType());
        }
        if(null!=trackQuery.getBeginTime()){
            query.setParameter("beginTime",trackQuery.getBeginTime());
        }
        if(null!=trackQuery.getEndTime()){
            query.setParameter("endTime",trackQuery.getEndTime());
        }
        if(null!=trackQuery.getOrgId()){
            Org org = dao.get(Org.class,trackQuery.getOrgId());
            query.setParameter("sysdept",org);
        }
        if(StringUtils.isNotEmpty(trackQuery.getOrgName())){
            query.setParameter("orgName","%"+trackQuery.getOrgName()+"%");
        }

        return query;
    }

    }
